<?php
	class ajaxAction extends userAction {
		private $feedSetting = false;
		protected function _initialize() {
			parent::_initialize();
			$this->feedSetting = getAddFeedSetting($this->uesrInfo->id,"infoaction"); 
		}
		
		public function savecontactsetting(){
			$field = $_POST["field"]."setting";
			$value = $_POST["value"];//post的数据一定安全么？
			$contactDao = D("user_contact");
			$contactDao->setField($field,$value,"userid=".$this->userInfo->id);
			echo "设置成功";
		}
		
		//添加工作信息
		public function saveworkinfo(){
			$workDao = D("user_work");
			$id = $_POST["id"];
			$company = htmlspecialchars($_POST["company"],ENT_QUOTES);
			$starttime = strtotime($_POST["starttime"]);
			$endtime = htmlspecialchars($_POST["endtime"],ENT_QUOTES);
			$endtime == "现在" ? $endtime = 0 : $endtime = strtotime($endtime);
			$desc = htmlspecialchars($_POST["desc"],ENT_QUOTES);
			if($id == 0){//添加
				$workDao->userid = $this->userInfo->id;
				$workDao->company = $company;
				$workDao->starttime = $starttime; 
				$workDao->endtime = $endtime;
				$workDao->desc = $desc;
				$newId = $workDao->add();
				if($newId>0){
					if($this->feedSetting){
						$content = "添加了工作信息";
						addFeed($this->userInfo->id,"editinfo",$content);
					}
					echo $newId;
				}else{
					echo 0;
				}
			}else{//保存
				$workDao->find("id=".$id." and userid=".$this->userInfo->id);
				$workDao->company = $company;
				$workDao->starttime = $starttime; 
				$workDao->endtime = $endtime;
				$workDao->desc = $desc;
				if($workDao->save()){
					if($this->feedSetting){
						$content = "更新了工作信息";
						addFeed($this->userInfo->id,"editinfo",$content);
					}
					echo $id;
				}else{
					echo 0;
					//$this->error("错误");
				}
			}
		}
		
		public function delwork(){
			$id = safeRequest($_POST["id"]);
			$workDao = D("user_work");
			$workDao->delete("id=".$id." and userid=".$this->userInfo->id);
			echo $id;
		}
		
		public function saveedu(){
			$eduDao = D("user_edu");
			$eduDao->create();
			$eduDao->userid = $this->userInfo->id;
			$id= $eduDao->add();
			if($id>0){
				if($this->feedSetting){
					$content = "添加了学校信息";
					addFeed($this->userInfo->id,"editinfo",$content);
				}
				echo $id;
			}else{
				echo 0;
			}
		}
		
		public function updateedu(){
			$eduDao = D("user_edu");
			$id = $_POST["id"];
			if($eduDao->find("id=".$id." and userid=".$this->userInfo->id)){
				$eduDao->create();
				if($eduDao->save()){
					if($this->feedSetting){
						$content = "更新了学校信息";
						addFeed($this->userInfo->id,"editinfo",$content);
					}
					echo "success";
				}else{
					echo "error";
				}
			}else{
				echo "error";
			}
		}
		
		public function deledu(){
			$id = $_GET["id"];
			if(D("user_edu")->delete("id=".$id." and userid=".$this->userInfo->id)){
				echo "success";
			}else{
				echo "error";
			}
		}
		/*
		public function saverevert(){	
			$blogid = safeRequest($_POST["blogid"]);			
			$revertDao = D("blogs_reverts");
			$requestid = $_POST["userid"];
			$content = $_POST["content"];
			$revertDao->blogid = $blogid;
			$revertDao->userid = $this->userInfo->id;
			$revertDao->revertid = $requestid;
			$revertDao->content = $content;
			$revertDao->regtime = time();
			$id = $revertDao->add();
			if($id > 0){
				D("blogs")->setField("revertnum","(revertnum+1)","id=".$blogid);
				$reid = $_POST["reid"];
				if($requestid != $this->userInfo->id){
					if($reid==0){
						addNotify($requestid,$this->userInfo->id,"blogrevert",$blogid,$id);
					}else{
						addNotify($requestid,$this->userInfo->id,"blogrevert2",$blogid,$id);
					}
				}
				if($reid != 0 && $reid != $requestid && $reid != $this->userInfo->id){
					addNotify($reid,$this->userInfo->id,"blogrevert2",$blogid,$id);
				}
				addFeed($this->userInfo->id,"blogrevert",$content,$blogid,$id);
				echo $id.",".$this->userInfo->id.",".$this->userInfo->username;
			}else{
				
				echo "error";
			}
		}

		public function savephotorevert(){
			$photoid = safeRequest($_POST["photoid"]);
			$reid = $_POST["reid"];
			$photo = D("albums_photos");
			$record = $photo->find("id=".$photoid,"albumid,userid");
			if($record){
				$revertDao = D("albums_photos_reverts");
				$albumid = $record->albumid;
				$userid = $record->userid;
				$content = $_POST["content"];
				$revertDao->photoid = $photoid;
				$revertDao->albumid = $albumid;
				$revertDao->userid = $userid;
				$revertDao->revertsid = $this->userInfo->id;
				$revertDao->postid = $reid;
				$revertDao->content = $content;
				$revertDao->regtime = time();
				$id = $revertDao->add();
				if($id > 0){
					$photo->setField("revertnum","(revertnum+1)","id=".$photoid);
					if($userid != $this->userInfo->id){
						if($reid==0){
							addNotify($userid,$this->userInfo->id,"photorevert",$photoid,$id);
						}else{
							addNotify($userid,$this->userInfo->id,"photorevert2",$photoid,$id);
						}
					}
					if( $reid > 0 && $reid != $userid && $reid != $this->userInfo->id){
						addNotify($_POST["reid"],$this->userInfo->id,"photorevert2",$photoid,$id);
					}
					addFeed($this->userInfo->id,"photorevert",$content,$photoid,$id);
					echo $id.",".$this->userInfo->id.",".$this->userInfo->username;
				}else{
					echo "error2";
				}
			}else{
				print "error1";
			}
		}
		
		public function delrevert(){
			$revertid = safeRequest($_POST["revertid"]);
			$dao = D("blogs_reverts");
			$record = $dao->find("id=".$revertid,"blogid,userid,revertsid,postid");
			if($record){
				if($record->userid == $this->userInfo->id || $record->revertsid = $this->userInfo->id)){
					$dao->delete("id=".$revertid);
					D("blogs")->setField("revertnum","(revertnum-1)","id=".$record->blogid);
					if($record->postid == 0){
						delFeed(0,$this->userInfo->id,"blogrevert",0,$revertid);
					}else{
						delFeed(0,$this->userInfo->id,"blogrevert2",0,$revertid);
					}
					echo "success";
				}else {
					echo "error";
				}
			}else{
				echo "error";
			}
		}
		
		public function delphotorevert(){
			$revertid = safeRequest($_POST["revertid"]);
			//$photoid = safeRequest($_POST["photoid"]);
			$dao = D("albums_photos_reverts");
			$record = $dao->find("id=".$revertid,"photoid,userid,revertsid,postid");
			if($record){
				if($record->userid == $this->userInfo->id || $record->revertsid = $this->userInfo->id)){
					$dao->delete("id=".$revertid);
					D("albums_photos")->setField("revertnum","(revertnum-1)","id=".$record->photoid);
					if($record->postid == 0){
						delFeed(0,$this->userInfo->id,"photorevert",0,$revertid);
					}else{
						delFeed(0,$this->userInfo->id,"photorevert2",0,$revertid);
					}
					echo "success";
				}else {
					echo "error";
				}
			}else{
				echo "error";
			}
			
		}
		*/
		public function saveprivacysetting(){
			$field = $_POST["field"];
			$value = $_POST["value"];//post的数据一定安全么？
			$dao = D("user_setting");
			if($dao->setField($field,$value,"userid=".$this->userInfo->id)){
				$record = $dao->find("userid=".$this->userInfo->id);
				//$path = DATA_PATH.$userId;
				F("userSetting",$record,DATA_PATH.$this->userInfo->id); //重新写cahce
				echo "success";
			}else{
				echo "error";
				//$this->error("操作失败");
				//echo $dao->getError();
				//$this->error($dao->getError()); 
			}
		}
		
		public function saveinfo(){
			$field = $_POST["field"];
			if("username" == $field){
				$value = trim($_POST["value"]);
				if($value == $this->userInfo->username){
					echo "已设置成功";
					return;
				}else{
					$dao = D("user");
					if($dao->setField($field,$value,"id=".$this->userInfo->id)){
						$content = $this->userInfo->username.'把名字更改了为<a href="'.__APP__.'/home/'.$this->userInfo->id.'">'.$value.'</a>';
						//$userInfo = $dao->find("id=".$this->userInfo->id);
						//Session::set("userInfo",$userInfo);
						addFeed($this->userInfo->id,"editname",$content);
						echo "设置成功";
						return; 
					}else{
						echo "出现异常，你可以重试一下!";
						return ;
					}
				}
			}elseif ("password" == $field){
				$oldpassword = trim($_POST["oldpassword"]);
				if(md5($oldpassword) != $this->userInfo->password){
					echo "旧密码输入不正确，请重试。";
					return;
				}			
				$password = trim($_POST["password"]);
				$password2 = trim($_POST["password2"]);
				if(strlen($password)<6){
					echo "密码不能少于6个字符";
					return;
				}
				if($password != $password2){
					echo "两次密码输入不一致";
					return;
				}
				$dao = D("user");
				if($dao->setField($field,md5($password),"id=".$this->userInfo->id)){
					echo "设置成功";
					return; 
				}else{
					echo "出现异常，你可以重试一下!";
					return ;
				}
			}elseif ("email" == $field){
				$value = trim($_POST["value"]);
				$dao = D("user");
				if($dao->setField($field,$value,"id=".$this->userInfo->id)){
					$userInfo = $dao->find("id=".$this->userInfo->id);
					Session::set("userInfo",$userInfo);
					echo "设置成功";
					return; 
				}else{
					echo "出现异常，你可以重试一下!";
					return ;
				}
			}
		}
	}
?>